package com.google.coffeeshop.gui.desktop.product;


import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import com.google.coffeeshop.business.LoadingBusiness;

import com.google.coffeeshop.entity.ProductDTO;
import com.google.coffeeshop.entity.ProductGroupDTO;
import com.google.coffeeshop.gui.desktop.ContentBasePanel;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;

import javax.swing.JScrollPane;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.RowSpec;
import com.jgoodies.forms.factories.FormFactory;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.List;

import javax.swing.JTextField;

public class ProductPanel extends ContentBasePanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private ProductTable productTable;
	private JComboBox cbbProductGroup;
	private JButton btnFilter;
	private List<ProductGroupDTO> listProductGroup;

	
	
	public ProductPanel(String constraints) {
		super(constraints);
		setLayout(new FormLayout(new ColumnSpec[] {
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("max(0dlu;default):grow"),
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("max(169dlu;default)"),
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("max(67dlu;default)"),
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,},
			new RowSpec[] {
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,}));
		
	
		
		initializeGUI();
	}
	private void initializeGUI() {
		
		
		productTable = new ProductTable();
		productTable.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				if (e.getClickCount() == 2) {
					showProductForm();
				}
			}
		});
		
		btnFilter = new JButton("Filter");
		btnFilter.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				filterByProductGroup();
			}
		});
		
		cbbProductGroup = new JComboBox();
		add(cbbProductGroup, "5, 2, 2, 1, fill, default");
		setProductGroupModel();
		
		add(btnFilter, "8, 2");
		add(new JScrollPane(productTable), "1, 4, 8, 1, fill, default");
		putAllProductsToTable(productTable,LoadingBusiness.getAllProducts());
	}
	
	public void showProductForm() {
		long productID = getProductSelectedID();
		if(productID >0){
			ProductForm frmProduct = new ProductForm(this,productID);
			frmProduct.setVisible(Boolean.TRUE);
		}
	}
	private void putAllProductsToTable(ProductTable productTable, List<ProductDTO> listProduct) {
		productTable.addNewRows(listProduct);
	}
	public void refreshGUI() {
		removeAll();
		initializeGUI();
		validate();
		repaint();
	}
	public long getProductSelectedID(){
		return productTable.getProductSelected();
	}
	
	private void setProductGroupModel(){
		DefaultComboBoxModel model = new DefaultComboBoxModel();
		listProductGroup = LoadingBusiness.getAllProductGroup();
		for(ProductGroupDTO group: listProductGroup){
			model.addElement(group.getName());	
		}
		
		cbbProductGroup.setModel(model);
	}
	
	private void filterByProductGroup(){
		long groupId = listProductGroup.get(cbbProductGroup.getSelectedIndex()).getId();
		productTable.emptyProductTable();
		putAllProductsToTable(productTable,LoadingBusiness.getActiveByGroupId(groupId));
	}


}
